package com.navi168.fwq21.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.apache.mina.core.session.IoSession;
import org.za.communication.core.handler.SessionIdleHandler;
import org.za.communication.message.WhereGoMessage;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.mapapi.core.GeoPoint;
import com.navi168.fwq21.R;
import com.navi168.fwq21.db.Sqlh;
import com.navi168.fwq21.hyzl.SendList;
import com.navi168.fwq21.hyzl.ShowMap;
import com.navi168.fwq21.hyzl.mod.Favo;
import com.navi168.fwq21.hyzl.mod.MypoiItems;
import com.navi168.fwq21.hyzl.mod.Xfhistroy;

public class SendPosition extends Activity {
	private EditText postnum;
	private EditText mddjc;
	private EditText mddqc;
	private EditText message;//
	private TextView jwd;// 经纬度
	private CheckBox isaddfav;// 是否加入兴趣点
	private Button okButton;
	private Button noButton;
	private MypoiItems poiItems;
	private String personname;
	private String postnumString;
	private ArrayList<Parcelable> nums;
	private int lon;
	private int lat;
	private boolean ischose;
	private Xfhistroy xfhistroy;
	private Sqlh sqlh;
	private IoSession session = SessionIdleHandler.ioSession;
	private Favo favo;
	private ProgressDialog progDialog;
	private int flag;
	private long message_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.xiafa);
		sqlh = sqlh.getInstance(SendPosition.this);
		findviews();
		setlisers();

		Bundle bundle = this.getIntent().getBundleExtra("sendlist");
		poiItems = (MypoiItems) bundle.getParcelable("poi_xf");
		postnumString = bundle.getString("postnum");
		postnum.setText(postnumString);
		SharedPreferences preferences = this.getSharedPreferences(
				Constants.SPF, Context.MODE_WORLD_READABLE);
		personname = preferences.getString("mymoblie", "");
//		personname = bundle.getString("postname");
		mddjc.setText(poiItems.getTitle());
		mddqc.setText(poiItems.getSnippet());
		lon = poiItems.getLongit();
		lat = poiItems.getLatit();
		jwd.setText(lon + "," + lat);

	}

	private void setlisers() {

		isaddfav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					ischose = true;
				} else {
					ischose = false;
				}
			}
		});

		noButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SendPosition.this.finish();

			}
		});

		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				saveHis();
				if (ischose) {
					addToFav();
				}
				zhiLu();
			}
		});

	}

	private void findviews() {

		postnum = (EditText) findViewById(R.id.xf_khh_nr);
		mddjc = (EditText) findViewById(R.id.xf_mddjc_nr);
		mddqc = (EditText) findViewById(R.id.xf_mddqc_nr);
		message = (EditText) findViewById(R.id.xf_fy_nr);
		okButton = (Button) findViewById(R.id.xf_ok_button);
		noButton = (Button) findViewById(R.id.xf_cance_button);
		isaddfav = (CheckBox) findViewById(R.id.isadd);
		jwd = (TextView) findViewById(R.id.xf_zuobiao);
		progDialog = new ProgressDialog(this);

	}

	private void saveHis() {

		xfhistroy = new Xfhistroy();
		xfhistroy.setHistroy_flag("1");// 1是发送，0是接收
		xfhistroy.setHistroy_fy(message.getText().toString());
		xfhistroy.setHistroy_jondate(new Tools().getnowtime());
		xfhistroy.setHistroy_lat(lat);
		xfhistroy.setHistroy_long(lon);
		xfhistroy.setHistroy_number(postnum.getText().toString());
		xfhistroy.setHistroy_person(personname);
		xfhistroy.setHistroy_snippet(mddqc.getText().toString());
		xfhistroy.setHistroy_title(mddjc.getText().toString());
		int noti = sqlh.saveHis(xfhistroy);
		Log.i("mylog", noti + "noti");
	}

	private void addToFav() {
		favo = new Favo();
		favo.setFav_lat(lat);
		favo.setFav_lon(lon);
		favo.setFav_snipeet(mddqc.getText().toString());
		favo.setFav_title(mddjc.getText().toString());
		favo.setJoin_date(new Tools().getnowtime());
		int noti = sqlh.add(favo);
		Log.i("mylog", "noti" + noti);
	}

	private void zhiLu() {
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				senddata();
			}

		});
		progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progDialog.setIndeterminate(false);
		progDialog.setCancelable(true);
		progDialog.setMessage("位置信息提交中...");
		progDialog.show();
		thread.start();
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				progDialog.dismiss();
				showToast("发送已成功！");
				Intent intent = new Intent(SendPosition.this, ShowMap.class);
				startActivity(intent);
				SendPosition.this.finish();
			}
			if (msg.what == 1) {
				progDialog.dismiss();
				showToast("发送失败！接受号码错误！！！");
			}
			if (msg.what == 2) {
				progDialog.dismiss();
				showToast("发送失败！发送的地址错误！！！");
			}
			if (msg.what == 3) {
				progDialog.dismiss();
				showToast("发送失败！请稍候在尝试！！！");

			}
		}
	};

	private void senddata() {
		WhereGoMessage wgm = new WhereGoMessage();
		wgm.setAsnippet(mddqc.getText().toString().trim());
		wgm.setAtitle(mddjc.getText().toString().trim());
		wgm.setLat(lat);
		wgm.setLog(lon);
		wgm.setReceiver(postnumString.replace(" ", ","));
		wgm.setSender(personname);
		wgm.setRemarks(message.getText().toString());
		wgm.setKey(SessionIdleHandler.key);
		session.write(wgm);
		while (true) {
			if (SessionIdleHandler.go == true) {
				flag = SessionIdleHandler.zhilu;
				message_id = SessionIdleHandler.message_id;
				SessionIdleHandler.go = false;
				handler.sendEmptyMessage(flag);
				break;
			}

		}
	}

	private void showToast(String showString) {
		Toast.makeText(getApplicationContext(), showString, Toast.LENGTH_SHORT)
				.show();
	}

}
